#include <string.h>
#include <stdio.h>
#include "avilib\avilib.h"

#define IMG_WIDTH     160
#define IMG_HEIGHT    120

void main(int argc, char argv[])
{ 	
	char VideoFmtMJPG[4] = 
	{
		'M','J','P','G'
	};
	char InputFileName[100];
	int i, ReadSize;
	unsigned long off_lengh;
	FILE *fpJPG;
	char input_jpg[4096];
	char input_buf[512];
	avi_t*  pAviHandle;
	
	printf("start\n");
	pAviHandle = AVI_open_output_file("anna.avi");
	if(!pAviHandle)
	{
		printf("avi file open failed \n");
		return;
	}
	AVI_set_video(pAviHandle, IMG_WIDTH, IMG_HEIGHT, 25, VideoFmtMJPG);

//	fpYUV = fopen("akiyo_300fs.qcif", "rb");
//	while(fread(input_yuv, 1, IMG_WIDTH*IMG_HEIGHT*3/2, fpYUV) > 0) 
//		AVI_write_frame(pAviHandle, input_yuv, IMG_WIDTH*IMG_HEIGHT*3/2);
//	fclose(fpYUV);

	int k=0;
	i=0;
	//for(i = 1; i < 70; i++)
	while(1)
	{
		k++;
		if(k>=100) break;
			
		printf("%d,i%5+1=%d\n", i, i%6+1);
		
			{
						sprintf(InputFileName, "frame%d.jpg", i%6+1);
			}
					i++;
		fpJPG = fopen(InputFileName, "rb");
		off_lengh = 0;
		fread(input_buf, 1, 10, fpJPG);
		memcpy(&input_jpg[off_lengh], input_buf, 10);
		input_jpg[6] = 'A';
		input_jpg[7] = 'V';
		input_jpg[8] = 'I';
		input_jpg[9] = '1';
		off_lengh += 10;
		while((ReadSize = fread(input_buf, 1, 512, fpJPG)) > 0)
		{
			memcpy(&input_jpg[off_lengh] , input_buf, ReadSize);
			off_lengh += ReadSize;
		}
		AVI_write_frame(pAviHandle, input_jpg, off_lengh);
//		AVI_bytes_remain(pAviHandle);
		fclose(fpJPG);
	}
//	AVI_dup_frame(pAviHandle);
	AVI_close(pAviHandle);
	printf("end\n");
}